// 封装axios
//1.先引入
//匹配的类型 { AxiosRequestConfig, AxiosResponse }
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";

//引入遮罩
import { ElLoading } from "element-plus";
//定义遮罩变量
let loading: any;
//遮罩的开始
const startLoading = () => {
  //1)定义类型
  interface Options {
    lock: boolean;
    text: string;
    background: string;
  }
  // 2）匹配类型
  const options: Options = {
    lock: true,
    text: "加载中...",
    background: "rgba(0,0,0,0.7)",
  };
  //赋值遮罩的开始
  loading = ElLoading.service(options);
};

//遮罩的结束
const endLoading = () => {
  loading.close();
};
// 请求拦截request
axios.interceptors.request.use((config: AxiosRequestConfig) => {
  // 加载开始
  startLoading();
  return config;
});

// 响应拦截 response
axios.interceptors.response.use(
  (response: AxiosResponse<any>) => {
    // 结束loading
    endLoading();
    return response;
  },
  (error) => {
    // 结束loading
    endLoading();
    // 错误提醒
    return Promise.reject(error);
  }
);
//2.再抛出（导出）
export default axios;
//3.全局main挂载http.ts
